# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        博智科技  
# Name:         demo1611
# Description:
# Author:       yzl
# Date:         2019-02-11
#-------------------------------------------------------------------------------


from urllib3 import *
from urllib.parse import urlencode
import re

def str2headers(file):
    headerDicts = {}
    f = open(file,'r')
    headerText = f.read()
    headers = re.split('\n',headerText)
    for header in headers:
        ret = re.split(':',header,maxsplit=1)
        headerDicts[ret[0]] = ret[1]

    return headerDicts

disable_warnings()

# 生成一个http对象
http = PoolManager()

"""
url = 'https://www.baidu.com/s?' + urlencode({
    'ie':'utf-8',
    'f':'3',
    'rsv_bp':'1',
    'rsv_idx':'1',
    'tn':'baidu',
    'wd':'湖南株洲  IT培训',
})
print(url)
rep = http.request('get',url)
print(rep.data)
# print(rep.headers)
print(rep.data.decode('utf-8'))
"""


headers = str2headers('headers.txt')
#print(headers)

url = 'https://www.baidu.com/s'
# 发起请求
req = http.request(
    'get', # 请求方式
    url=url, # 请求地址
    fields ={'wd':'株洲it培训'},# 请求参数
    headers=headers # 请求头,模拟什么浏览器
)

# 读取请求的数据
page = req.read()
print(page)
print(req.data.decode('utf-8'))


